package com.xiebishe.mylibrary.common.glide.glide;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.os.Looper;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.disklrucache.DiskLruCache;
import com.bumptech.glide.load.Option;
import com.bumptech.glide.load.engine.cache.SafeKeyGenerator;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.EmptySignature;
import com.xiebishe.mylibrary.base.MyApplication;
import com.xiebishe.mylibrary.common.glide.DataCacheKey;
import com.xiebishe.mylibrary.common.glide.FileUtils;
import com.xiebishe.mylibrary.common.glide.IImageLoaderStrategy;

import java.io.File;
import java.io.IOException;

public class GlideImageLoaderStrategy implements IImageLoaderStrategy {
    public static final Option<Boolean> NEED_CUTTING = Option.memory("NEED_CAST", false);
    public static final Option<Boolean> NEED_RESIZE = Option.memory("NEED_RESIZE", false);
    private static final String CACHE_PATH;

    public GlideImageLoaderStrategy() {
    }

    @SuppressLint({"CheckResult"})
    public void loadImage(Context context, String url, boolean isSvg, boolean isCircle, boolean isCenterCrop, boolean autoFitUrlToCutting, boolean autoFitUrlToSize, final Drawable placeHolder, Drawable errorHolder, ImageView imageView, int viewWidth, int viewHeight) {
        if (!(context instanceof Activity) || !((Activity)context).isFinishing() && !((Activity)context).isDestroyed()) {
            RequestOptions requestOptions = new RequestOptions();
            if (isCenterCrop) {
                requestOptions = requestOptions.centerCrop();
            }
            if (placeHolder != null) {
                requestOptions = (RequestOptions)requestOptions.placeholder(placeHolder);
            }

            if (errorHolder != null) {
                requestOptions = (RequestOptions)requestOptions.error(errorHolder);
            }

            if (viewWidth > 0 && viewHeight > 0) {
                requestOptions = (RequestOptions)requestOptions.override(viewWidth, viewHeight);
            }
            RequestManager requestManager = GlideApp.with(context);
            RequestBuilder requestBuilder;
            if (isSvg) {
                requestBuilder = requestManager.as(PictureDrawable.class);
            } else {
                requestBuilder = requestManager.asDrawable();
            }

            requestBuilder.set(NEED_CUTTING, autoFitUrlToCutting);
            requestBuilder.set(NEED_RESIZE, autoFitUrlToSize);
            requestBuilder = requestBuilder.load(url).apply(requestOptions);
            if (imageView != null) {
                requestBuilder.into(imageView);
            }

        }
    }

    public boolean checkImageExist(String url) {
        File file = this.getCacheFile(url);
        return file != null;
    }

    private File getCacheFile(String url) {
        DataCacheKey dataCacheKey = new DataCacheKey(new GlideUrl(url), EmptySignature.obtain());
        SafeKeyGenerator safeKeyGenerator = new SafeKeyGenerator();
        String safeKey = safeKeyGenerator.getSafeKey(dataCacheKey);

        try {
            int cacheSize = 100000000;
            DiskLruCache diskLruCache = DiskLruCache.open(new File(CACHE_PATH), 1, 1, (long)cacheSize);
            DiskLruCache.Value value = diskLruCache.get(safeKey);
            if (value != null) {
                return value.getFile(0);
            }
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        return null;
    }

    public void clearImageDiskCache(Context context) {
        GlideApp.get(context.getApplicationContext()).clearDiskCache();
    }

    public void clearImageMemoryCache(Context context) {
        try {
            if (Looper.myLooper() == Looper.getMainLooper()) {
               GlideApp.get(context.getApplicationContext()).clearMemory();
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

    }

    public void trimMemory(Context context, int level) {
        Glide.get(context).trimMemory(level);
    }

    public long getCacheSize(Context context) {
        try {
            return FileUtils.getFileOrFolderSize(CACHE_PATH);
        } catch (Exception var3) {
            var3.printStackTrace();
            return -1L;
        }
    }
    public void resumeRequest(Context context) {
        Glide.with(context).resumeRequestsRecursive();
    }

    static {
        CACHE_PATH = MyApplication.getInstance().getApplicationContext().getExternalCacheDir().getPath() + File.separatorChar + "image_manager_disk_cache";
    }
}

